/*外部框架的JS*/
$(function () {
    console.log('外层框架已加载...');
    $(".ajaxSubmit-btn").click(function () {
        //防止多此点击事件的执行
        if($("body").data('flg-btn')==0){
            $("body").data('flg-btn',1);
            $(this).parents('form').ajaxSubmit({
                dataType:'JSON',
                success:function (res) {
                    var jump = res.jump,jump_url = res.jump_url;
                    layer_msg(res,jump,jump_url);
                },
                complete:function () {
                    $("body").data('flg-btn',0);
                }
            });
        }
    });
    $(".ajax-btn").each(function () {
        var effect = $(this).data('effect')||0,
            url = $(this).data('url'),
            data = $(this).data('params')||{},
            type = $(this).data('type')||'get',
            pop = $(this).data('pop'),
            jump_url = $(this).data('jump_url');
        if(effect==1){
            if(url==undefined){parent.parent.layer.msg('请求链接未定义！')}
            else {
                $.ajax({
                    url:url,
                    type:type,
                    dataType:'JSON',
                    data:data,
                    success:function (res) {
                        var jump = res.jump;
                        if(pop == 'yes,no'){
                            layer_msg(res,jump,jump_url);
                        }
                        if(pop == 'yes'){
                            if(res.code==1){
                                layer_msg(res,jump,jump_url);
                            }
                        }
                        if(pop == 'no'){
                            if(res.code==0){
                                layer_msg(res,jump,jump_url);
                            }
                        }
                    }
                })
            }
        }
        else{
            $(this).click(function () {
                if($("body").data('flg-btn')==0){
                    $("body").data('flg-btn',1);
                    if(url==undefined){parent.parent.layer.msg('请求链接未定义！')}
                    else {
                        $.ajax({
                            url:url,
                            type:type,
                            dataType:'JSON',
                            data:data,
                            success:function (res) {
                                var jump = res.jump;
                                if(pop == 'yes,no'){
                                    layer_msg(res,jump,jump_url);
                                }
                                if(pop == 'yes'){
                                    if(res.code==1){
                                        layer_msg(res,jump,jump_url);
                                    }
                                }
                                if(pop == 'no'){
                                    if(res.code==0){
                                        layer_msg(res,jump,jump_url);
                                    }
                                }
                            },
                            complete:function () {
                                $("body").data('flg-btn',0);
                            }
                        })
                    }
                }
            })
        }
    });
    //样式变量
    var dustar_side_menu = $(".dustar-side-menu"),//侧边栏
        layui_footer = $(".layui-footer"),//底部
        layui_body = $(".layui-body"),//身体
        content = $('#navbar .layui-tab-content'),//iframe 栏目
        site_mobile_shade = $(".dustar-body .layui-layout-admin .site-mobile-shade");//移动端阴影;
    //全局变量
    var this_url = window.location.href;
    //公共变量
    //PC端 侧栏伸缩
    $(".admin-side-toggle").click(function () {
        if(dustar_side_menu.hasClass('dustar-side-hide')){
            dustar_side_menu.removeClass('dustar-side-hide');
            layui_footer.removeClass('dustar-footer');
            layui_body.removeClass('dustar-body1');
            site_mobile_shade.hide();
        }
        else{
            dustar_side_menu.addClass('dustar-side-hide');
            layui_footer.addClass('dustar-footer');
            layui_body.addClass('dustar-body1');
            site_mobile_shade.hide();
        }
    });
    //移动端 侧栏伸缩
    site_mobile_shade.click(function () {
        $(this).hide();
        dustar_side_menu.addClass('dustar-side-mobile-hide');
        dustar_side_menu.removeClass('dustar-side-hide');
    });
    $(".site-tree-mobile").click(function () {
        site_mobile_shade.show();
        dustar_side_menu.removeClass('dustar-side-hide');
        dustar_side_menu.removeClass('dustar-side-mobile-hide');
    });

    $("#left_nav li a").click(function () {
        if($(this).siblings('dl').length==0){
            $("#person_pri dd").removeClass('layui-this');
        }
    });

    //头部右侧的选项卡和侧边菜单UI不冲突
    $("#person_pri dd").click(function () {
        if(!$(this).find('a').hasClass('no-generate')){
            $("#left_nav li").removeClass('layui-this');
            $("#left_nav dd").removeClass('layui-this');
            var layid = $(this).find('a').data('href'),title = $(this).find('cite').html(),
                icon=$(this).find('i').prop('class')||'layui-icon layui-icon-file';
            title = '<i class="'+ icon +'"></i><span>'+title+'</span>';
            generate(layid,title);
        }
    });

    //栏目选项
    dustar_side_menu.find('ul a').click(function () {
        var is_mobile = isMobile();
        var is_child = $(this).siblings('dl').length;
        if(is_mobile&&!is_child){
            site_mobile_shade.hide();
            dustar_side_menu.addClass('dustar-side-mobile-hide');
        }
        if(!is_child){
            var layid = $(this).data('href'),title = $(this).find('cite').html(),
                icon=$(this).find('i').prop('class')||'layui-icon layui-icon-file';
            title = '<i class="'+ icon +'"></i><span>'+title+'</span>';
            generate(layid,title);
        }
    });

    var system_module = $("#system_module dl dd");
    for(var i=0;i<system_module.length;i++){
      var url = system_module.eq(i).find('a').attr('href');
      if(this_url == url){
          system_module.eq(i).addClass('layui-this');
      }
    }
    //iframe自适应
    $(window).on('resize', function() {
        content.height($(window).height() - 140);
        content.find('.layui-tab-item').each(function() {
            $(this).height(content.height());
        });
    }).resize();


    function generate(layid,title) {
        var count = 0;
        var li = $("#navbar .layui-tab-title li");
        for(var i=0;i<li.length;i++){
            var _layid = li.eq(i).attr('lay-id');
            if(_layid==layid){
                element.tabChange('navbar',layid);
            }
            else {
                count++
            }
        }
        if(li.length==count){
            element.tabAdd('navbar', {
                title: title,
                content: '<iframe class="iframe-windows" src="'+ layid +'"></iframe>', //支持传入html
                id: layid
            });
            element.tabChange('navbar',layid);
        }
        content.find('.layui-tab-item').height($(window).height() - 140);
    }


});

